const XEUtils = require('xe-utils')

/**
 * Mainly used for data list paging url param handle
 * @param getListApi => api function(required)
 */

export default {
  data() {
    return {
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 1,
      pageSizeOptions: [10, 30, 50],
      listLoading: false,
      searchParams: {
        pageSize: 10,
        pageNum: 1,
      }
    }
  },
  watch: {
    // watch pageNum
    searchParams: {
      deep: true,
      handler() {
        this._debounceGET()
      }
    }
  },
  mounted() {
    // Routing parameter merging
    if (!XEUtils.isEmpty(this.$route.query)) {
      this.searchParams = XEUtils.assign(this.searchParams, this.$route.query)
    }
    this._debounceGET()
  },
  methods: {
    resetClick(val) {
      // 重置
      if (JSON.stringify(this[val]) === '{}') {
        return
      }
      this.clearParams(val)
    },
    clearParams(val) {
      this.datePicker = null
      this[val] = {}
      this.searchParams = {
        pageSize: 10,
        pageNum: 1,
      }
    },
    searchConditions(val) {
      // 列表搜索  判断值有没变化变化则触发搜索
      if (!val && JSON.stringify(this.val) === '{}') {
        return
      }
      const bool = Object.keys(val).some(e => {
        return val[e] !== this.searchParams[e]
      });

      if (bool) {
        this.searchParams = { ...this.searchParams, ...val, pageNum: 1 };
      }
    },

    pageFun(val) {
      // 分页
      this.searchParams.pageNum = val
    },
    pageSizeFun(val) {
      // 改变size
      this.searchParams.pageSize = val
    },
    _debounceGET: XEUtils.debounce(function (flag) {
      this.getListApi(flag)
    }, 100, {
      leading: false,
      trailing: true
    })
  }
}
